package com.cyf.db.mapper;

import com.cyf.db.bean.LocalPerson;
import com.cyf.db.bean.Person;
import org.apache.ibatis.annotations.*;

import java.util.List;


public interface PersonMapper {

	@Select("select * from local_person where id = #{id}")
    Person getById(@Param("id") int id);

    @Select("select * from local_person where phone = #{phone} and status = 1 ")
    List<Person> getByPhone(@Param("phone") String phone);

    @Select("select * from local_person where status = 1 and localPersonId = #{localPersonId} and parkingId = #{parkingId} limit 0, 1")
    Person getByLocalPersonIdAndParkingId(@Param("localPersonId") Integer personId, @Param("parkingId") Integer parkingId);

    @Select("select carportnum from local_person where status = 1 and localPersonId = #{localPersonId} and parkingId = #{parkingId} limit 0, 1")
    Integer getCarportNumByPersonIdAndParkingId(@Param("localPersonId") Integer personId, @Param("parkingId") Integer parkingId);

    @Select("<script>" +
            "select p.id, p.localPersonId, p.name,p.phone " +
            "from local_person p " +
            "LEFT JOIN local_car c ON p.localPersonId = c.personId and c.`status` = 1 and c.statusbit = 1 and c.parkingId = #{parkingId} " +
            "where p.parkingId = #{parkingId} and p.`status` = 1 " +
            "<if test=\"personName != null and personName != ''\">" +
            "   and p.`name` like '${personName}%' " +
            "</if>" +
            "<if test=\"plate != null and plate != ''\">" +
            "   and c.`license` like '${plate}%' " +
            "</if>" +
            "GROUP BY p.localPersonId " +
            "order by p.localPersonId desc  " +
            "limit #{pageNum}, #{pageSize}" +
            "</script>")
    List<LocalPerson> listBaseInfo(@Param("parkingId") Integer parkingId,
                                   @Param("personName") String personName,
                                   @Param("plate") String plate,
                                   @Param("pageNum") int pageNum,
                                   @Param("pageSize") int pageSize);

    @Select("select localPersonId, parkingId from local_person " +
            "where id = #{id}")
    LocalPerson getLocalIdAndParkingIdById(@Param("id") Integer id);

    @Update("update local_person set status = 0 where id = #{id}")
    void logicDeleteById(@Param("id") Integer id);


    @Select("select carportnum from local_person where id = #{id}")
    Integer getCarportNumById(@Param("id") Integer id);


}
